﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DemoGeometry
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            this.printDocument1.OriginAtMargins = true;
        }

        private void btnLine_Click(object sender, EventArgs e)
        {
            if (this.linearControl1.IsValid())
            {
                var a = this.linearControl1.A;
                var b = this.linearControl1.B;
                var c = this.linearControl1.C;
                //判断方程的参数，是否有效
                LinearEquation linear = new LinearEquation() { A = a, B = b, C = c };
                if (!linear.IsValid())
                {
                    MessageBox.Show("输入的方程参数无效");
                    return;
                }
                if (!this.axisControl1.CheckLineIsValid(linear))
                {
                    MessageBox.Show("输入的方程不在坐标轴内");
                    return;
                }
                bool flag = this.axisControl1.GenerateLinear(linear);
                if (!flag)
                {
                    MessageBox.Show("生成直线失败");
                    return;
                }
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.axisControl1.Clear();
        }

        private void btnPoint_Click(object sender, EventArgs e)
        {
            if (this.pointControl1.IsValid())
            {
                float x = this.pointControl1.X;
                float y = this.pointControl1.Y;
                PointF point = new PointF(x, y);
                if (!this.axisControl1.CheckPointIsValid(point)) {
                    MessageBox.Show("输入的点不在坐标轴内");
                    return;
                }
                bool flag = this.axisControl1.GeneratePoint(point);
                if (!flag)
                {
                    MessageBox.Show("生成点失败");
                    return;
                }
            }
            
        }

        private void btnParabolic_Click(object sender, EventArgs e)
        {
            if (this.parabolicControl1.IsValid())
            {
                var a = this.parabolicControl1.A;
                var b = this.parabolicControl1.B;
                var c = this.parabolicControl1.C;
                //判断方程的参数，是否有效
                ParabolicEquation parabolic = new ParabolicEquation() { A = a, B = b, C = c };
                if (!parabolic.IsValid())
                {
                    MessageBox.Show("输入的方程参数无效");
                    return;
                }
                if (!this.axisControl1.CheckParabolicIsValid(parabolic))
                {
                    MessageBox.Show("输入的方程不在坐标轴内");
                    return;
                }
                bool flag = this.axisControl1.GenerateParabolic(parabolic);
                if (!flag)
                {
                    MessageBox.Show("生成抛物线失败");
                    return;
                }
            }
        }

        private void btnPolygon_Click(object sender, EventArgs e)
        {
            List<PointF> lstPoints = new List<PointF>() {
                new PointF(1,2),
                new PointF(2,0),
                new PointF(1,-2),
                new PointF(-1,-2),
                new PointF(-2,0),
                new PointF(-1,2)
            };
            this.axisControl1.GeneratePolygon(lstPoints);
            //再画一个多边形
            lstPoints = new List<PointF>() {
                new PointF(5,6),
                new PointF(6,4),
                new PointF(5,2),
                new PointF(3,2),
                new PointF(2,4),
                new PointF(3,6)
            };
            this.axisControl1.GeneratePolygon(lstPoints);
        }

        private void btnExpand_Click(object sender, EventArgs e)
        {
            List<PointF> lstPoints = new List<PointF>() {
                new PointF(1,2),
                new PointF(2,0),
                new PointF(1,-2),
                new PointF(-1,-2),
                new PointF(-2,0),
                new PointF(-1,2)
            };
            this.axisControl1.GenerateExpandPolygon(lstPoints, new PointF(0, 0), 0.5f);

            //再画一个多边形
            lstPoints = new List<PointF>() {
                new PointF(5,6),
                new PointF(6,4),
                new PointF(5,2),
                new PointF(3,2),
                new PointF(2,4),
                new PointF(3,6)
            };
            this.axisControl1.GenerateExpandPolygon(lstPoints, new PointF(4, 4), 0.5f);
        }

        private void bntPrint_Click(object sender, EventArgs e)
        {
            if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {

            Bitmap _NewBitmap = new Bitmap(axisControl1.Width, axisControl1.Height);
            axisControl1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
          //  _NewBitmap.Save(@"c:\abc.bmp");
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
           // e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height); 
            e.Graphics.DrawImage(_NewBitmap, e.Graphics.VisibleClipBounds); 
        }
    }
}
